﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<link href="~/css/stepcss.css" rel="stylesheet" />
<link href="~/js/lay-module/formDesigner/formDesigner.css" rel="stylesheet" />
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-carousel" id="adminform" lay-filter="adminform">
            <div carousel-item>
                <div>
                    <form class="layui-form layuimini-form">
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">表单名称</label>
                            <div class="layui-input-block">
                                <input type="text" id="F_Name" name="F_Name" required lay-verify="required"
                                       placeholder="表单名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">表单类型</label>
                            <div class="layui-input-block">
                                <select id="F_FrmType" name="F_FrmType" required lay-verify="required" lay-search lay-filter="FrmType">
                                    <option value="" selected>请选择</option>
                                    <option value="0">动态表单</option>
                                    <option value="1">自定义表单</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label">系统内置表单</label>
                            <div class="layui-input-block">
                                <select id="F_WebId" name="F_WebId" disabled lay-search lay-filter="WebId">
                                    <option value="" selected>请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label">归属部门</label>
                            <div class="layui-input-block">
                                <select id="F_OrganizeId" name="F_OrganizeId" lay-search>
                                    <option value="" selected>请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">排序</label>
                            <div class="layui-input-block">
                                <input type="number" pattern="[0-9]*" id="F_SortCode" name="F_SortCode" lay-verify="required|number" oninput="if(value.length>8)value=value.slice(0,8)" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label">选项</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="F_EnabledMark" id="F_EnabledMark" checked="" value="true" title="有效标识">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text layui-hide">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea id="F_Description" name="F_Description" class="layui-textarea" placeholder="请输入备注"></textarea>
                            </div>
                        </div>
                        <div class="form-group-bottom text-right">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formStep">
                                    &emsp;下一步&emsp;
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div>
                    <form class="layui-form layuimini-form">
                        <div id="commonform">
                            <div id="formdesigner">
                            </div>
                        </div>
                        <fieldset class="layui-elem-field">
                            <div id="frmPreview"></div>
                        </fieldset>
                        <div class="form-group-bottom text-right">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn layui-btn-primary pre">上一步</button>
                                <button class="layui-btn site-demo-active" id="saveBtn">
                                    &emsp;保存&emsp;
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="~/js/lay-module/formDesigner/Sortable/Sortable.min.js"></script>
<script type="text/javascript" charset="utf-8" src="~/js/lay-module/htmlformat.js"></script>
<script type="text/javascript" charset="utf-8" src="~/js/lay-module/jsformat.js"></script>
<script>
    //只读参数
    var readonly = true;
    layui.use(['jquery', 'form', 'common', 'element', 'step', 'formDesigner'], function () {
        var form = layui.form,
            $ = layui.$,
            common = layui.common,
            step = layui.step,
            element = layui.element;
        var formDesigner = layui.formDesigner;
        var keyValue = $.request("keyValue");
        step.render({
            elem: '#adminform',
            filter: 'adminform',
            width: '100%', //设置容器宽度
            stepWidth: '750px',
            height: '550px',
            stepItems: [{
                title: '基本信息'
            }, {
                title: '表单设计'
            }]
        });

        //权限字段
        common.authorizeFields('adminform');
        $(function () {
            initControl();
            if (!!keyValue) {
                common.ajax({
                    url: '/SystemManage/Form/GetFormJson',
                    dataType: 'json',
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                        if (data.F_FrmType != 0) {
                            $("#frmPreview").load(document.location.origin + '/form/' + data.F_WebId + '.html');
                            $('#commonform').addClass('layui-hide');
                            $('#frmPreview').removeClass('layui-hide');
                        }
                        else {
                            $('#commonform').removeClass('layui-hide');
                            $('#frmPreview').addClass('layui-hide');
                            //表单设计器
                            formDesigner.render({
                                data: JSON.parse(data.F_Content),
                                elem: '#formdesigner'
                            });
                        }
                    }
                });
            }
            else {
                common.ajax({
                    url: document.location.origin + '/json/demo.json',
                    success: function (result) {
                        formDesigner.render({
                            data: result,
                            elem: '#formdesigner'
                        });
                    }
                }); 
            }
            form.render();
        });
        wcLoading.close();
        function initControl() {
            $("#F_OrganizeId").bindSelect({
                url: "/SystemOrganize/Organize/GetTreeSelectJson",
            });
            $("#F_WebId").bindSelect({
                url: "/SystemManage/Form/GetExtendForm",
                id: "FileName",
                text: "FileName"
            });
        }
        $('.pre').click(function () {
            step.pre('#adminform');
        });

        $('.next').click(function () {
            step.next('#adminform');
        });
        form.on('select(FrmType)',
            function (data) {
                if (!!data.value && data.value == 1) {
                    $('#F_WebId').removeAttr("disabled");
                } else {
                    $('#F_WebId').attr("disabled", "disabled");
                    $('#F_WebId').val("");
                }
                form.render();
            });
        form.on('select(WebId)',
            function (data) {
                if (!!data.value) {
                    $("#frmPreview").load(document.location.origin + '/form/' + data.value + '.html');
                    form.render();
                }
                else {
                    $("#frmPreview").html("");
                }
            });
        var postData;
        form.on('submit(formStep)', function (data) {
            postData = $.extend(postData, data.field);
            if (postData.F_FrmType != 0) {
                $('#commonform').addClass('layui-hide');
                $('#frmPreview').removeClass('layui-hide');
            }
            else {
                $('#commonform').removeClass('layui-hide');
                $('#frmPreview').addClass('layui-hide');
            }
            step.next('#adminform');
            return false;
        });
        //监听提交
        $(document).on('click','#saveBtn', function () {
            //解析表单数据
            var formeditor = '';
            if (postData.F_FrmType != 1) {
                //获取表单设计器里的内容
                formeditor = document.getElementById('generate-code-view').value;
                if (!formeditor) {
                    common.modalMsg("请编辑设计器", "warning");
                    return false;
                }
                postData.F_Content = formeditor;
            }
            // 单击之后提交按钮不可选,防止重复提交
            $('.site-demo-active').addClass('layui-btn-disabled');
            $('.site-demo-active').attr('disabled', 'disabled');
            if (!postData["F_EnabledMark"]) postData["F_EnabledMark"] = false;
            common.submitForm({
                url: '/SystemManage/Form/SubmitForm?keyValue=' + keyValue,
                param: postData,
                success: function () {
                    common.reloadIframe("/SystemManage/Form/Index", 'data-search-btn');
                }
            })
            return false;
        });
    });
</script>

